﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DTO;
using Cocbook_DAL;

namespace Sale_and_manage
{
    public partial class frmProjectManage : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmProjectManage()
        {
            InitializeComponent();
            LoadAllProject();
            SetHeaderSize();
        }

        public void SetHeaderSize()
        {
            dgvProject.Columns[0].Width = 60;
            dgvProject.Columns[1].Width = 120;
            dgvProject.Columns[2].Width = 250;
            dgvProject.Columns[3].Width = 65;
            dgvProject.Columns[4].Width = 65;
            dgvProject.Columns[5].Width = 70;
            dgvProject.Columns[6].Width = 40;
        }

        public List<ProjectView> lpv = new List<ProjectView>();
        public int editRowsIndex = 0;

        public void LoadAllProject()
        {
            ConvertAndLoad((new ProjectDAL()).GetAllProject());
        }

        public void ConvertAndLoad(List<Project> lp)
        {
            if (lp != null)
            {
                lpv = new List<ProjectView>();
                foreach (Project p in lp)
                {
                    lpv.Add(new ProjectView(p));
                }
                dgvProject.DataSource = lpv;
            }
        }

        private void dgvProject_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lblBookInput.Text = (new Project_BookDAL()).GetBookInputByProjectID(Convert.ToInt16(dgvProject[0, e.RowIndex].Value)).ToString();
            lblTotalBookInput.Text = (new Project_BookDAL()).GetTotalBookInputByProjectID(Convert.ToInt16(dgvProject[0, e.RowIndex].Value)).ToString();
            lblTotalBookSold.Text = (new Project_BookDAL()).GetTotalBookSoldByProjectID(Convert.ToInt16(dgvProject[0, e.RowIndex].Value)).ToString();
            lblBookSold.Text = (new Project_BookDAL()).GetBookSoldByProjectID(Convert.ToInt16(dgvProject[0, e.RowIndex].Value)).ToString();
            lblQuatityOfBill.Text = (new ProjectDAL()).GetQuatityOfDealByProjectID(Convert.ToInt16(dgvProject[0, e.RowIndex].Value)).ToString();
            editRowsIndex = e.RowIndex;
            txtAddress.Text = dgvProject[2, e.RowIndex].Value.ToString();
            txtProjectName.Text = dgvProject[1, e.RowIndex].Value.ToString();
            txtStartDate.Value = DateTime.Parse(dgvProject[3, e.RowIndex].Value.ToString());
            txtEndDate.Value = DateTime.Parse(dgvProject[4,e.RowIndex].Value.ToString());
            txtTableNumber.Text = dgvProject[5,e.RowIndex].Value.ToString();
            switch (dgvProject[6,e.RowIndex].Value.ToString())
            {
                case "True":
                    cboStatus.SelectedIndex = 0;
                    break;
                case "False":
                    if (dgvProject[0, e.RowIndex].Value.ToString() == "1")
                    {
                        cboStatus.SelectedIndex = 2;
                    }
                    else
                    {
                        cboStatus.SelectedIndex = 1;
                    }
                    break;
                
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (btnEdit.Text == "Hủy")
            {
                txtAddress.Enabled = !true;
                txtEndDate.Enabled = !true;
                txtProjectName.Enabled = !true;
                lblQuatityOfBill.Enabled = !true;
                txtStartDate.Enabled = !true;
                txtTableNumber.Enabled = !true;
                cboStatus.Enabled = !true;
                dgvProject.Enabled = !false;
                btnEdit.Text = "Sửa";
                btnSave.Enabled = false;
                dgvProject_CellClick(null, new DataGridViewCellEventArgs(0, editRowsIndex));
            }
            else
            {
                txtAddress.Enabled = true;
                txtEndDate.Enabled = true;
                txtProjectName.Enabled = true;
                lblQuatityOfBill.Enabled = true;
                txtStartDate.Enabled = true;
                txtTableNumber.Enabled = true;
                btnSave.Enabled = true;
                if (cboStatus.SelectedIndex != 2)
                {
                    cboStatus.Enabled = true;
                }
                dgvProject.Enabled = false;
                btnEdit.Text = "Hủy";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtProjectName.Text == "")
            {
                highlighter1.SetHighlightColor(txtProjectName, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                lblValidate.Text = "Tên hội sách rỗng.";
            }
            else
            {
                if (txtAddress.Text == "")
                {
                    highlighter1.SetHighlightColor(txtAddress, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                    lblValidate.Text = "Địa điểm tổ chức rỗng.";
                    txtAddress.Focus();
                }
                else
                {
                    if (txtStartDate.Text == "")
                    {
                        highlighter1.SetHighlightColor(txtStartDate, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                        lblValidate.Text = "Ngày diễn ra ra rỗng.";
                        txtStartDate.Focus();
                    }
                    else
                    {
                        if (txtEndDate.Text == "" || txtEndDate.Value <= txtStartDate.Value)
                        {
                            highlighter1.SetHighlightColor(txtEndDate, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                            lblValidate.Text = "Ngày kết thúc không hợp lệ.";
                            txtEndDate.Focus();
                        }
                        else
                        {
                            if (txtTableNumber.Text == "")
                            {
                                highlighter1.SetHighlightColor(txtTableNumber, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                                lblValidate.Text = "Số bàn trong hội sách rỗng.";
                                txtTableNumber.Focus();
                            }
                            else
                            {
                                if (cboStatus.Enabled && cboStatus.SelectedIndex == 2)
                                {
                                    highlighter1.SetHighlightColor(cboStatus, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                                    lblValidate.Text = "Không thể chọn trạng thái này cho hội sách.";
                                    cboStatus.SelectedIndex = 1;
                                }
                                else
                                {
                                    bool status = false;
                                    if (cboStatus.SelectedIndex == 0)
                                    {
                                        if (MessageBox.Show("Bạn có muốn khởi động hội sách này hay không", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                        {
                                            status = true;
                                            Project p = (new ProjectDAL()).GetProjectByStatus();
                                            p.Status = false;
                                            (new ProjectDAL()).UpdateProject(p);
                                        }
                                    }
                                    (new ProjectDAL()).UpdateProject(new Project(Convert.ToInt16(dgvProject[0, editRowsIndex].Value), txtProjectName.Text, txtAddress.Text, DateTime.Parse(txtStartDate.Text), DateTime.Parse(txtEndDate.Text), int.Parse(txtTableNumber.Text), status));
                                    LoadAllProject();
                                    btnEdit_Click(null, null);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }

        private void txtProjectName_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtProjectName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtAddress_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtAddress, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtStartDate_ValueChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtStartDate, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtEndDate_ValueChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtEndDate, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtTableNumber_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtTableNumber, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void cboStatus_Click(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(cboStatus, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

    }
}